﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;
using System.Drawing.Imaging;
using ESRI.ArcGIS.Carto;
using GISSystem.WindowsForm;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;

namespace GISSystem.Class
{
    /// <summary>
    /// 用于控制主窗口界面的显示
    /// </summary>
    public class MainFormUI
    {
        frmMain mainForm;
        Color formColor;  //用于记录现在主界面颜色
        frm_Overview overviewForm;

        public Color FormColor
        {
            get { return formColor; }
        }

        public frm_Overview OverviewForm
        {
            get { return overviewForm; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="frm">主窗体</param>
        public MainFormUI(frmMain frm)
        {
            this.mainForm = frm;

            //新建一个缩略图窗口对象
            overviewForm = new frm_Overview(frm.mapOp, this);
            overviewForm.Owner = frm;
        }

        /// <summary>
        /// 初始化主界面
        /// </summary>
        public void InitMainForm()
        {
            //读取配置文件，暂留
            Function.GetConfiguration();

            //根据用户权限初始化界面，暂留

            //设置主菜单样式
            InitStyleMenu();  //初始化界面风格菜单

            //初始化AE的控件菜单
            mainForm.tocMenu = new ToolbarMenuClass();
            mainForm.tocMenu.SetHook(mainForm.mapControl);
            mainForm.tocMenu.ThemedDrawing = true;

            //显示缩略图
            if (Configuration.OverviewFormLocation.X == -1 && Configuration.OverviewFormLocation.Y == -1)
            {
                //指定位置，显示在右下角
                System.Drawing.Point loc = new System.Drawing.Point(mainForm.Width - overviewForm.Width - 25, mainForm.Height - overviewForm.Height - 45);
                overviewForm.Location = loc;
            }
            else
            {
                overviewForm.Location = Configuration.OverviewFormLocation;
            }
            overviewForm.Width = Configuration.OverviewFormWidth;
            overviewForm.Height = Configuration.OverviewFormHeight;

            if (Configuration.ShowOverviewForm)
            {
                mainForm.menuOverview.Checked = true;

                overviewForm.Show();
            }

            //按钮符号图像
            InitEquipmentGallery();
        }

        #region 界面风格菜单部分功能
        /// <summary>
        /// 更换界面的颜色风格
        /// </summary>
        /// <param name="item"></param>
        public void ChangeStyleColor(ButtonItem item)
        {
            Color clr = (Color)item.Tag;

            if (clr == Color.Blue)
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(mainForm, eOffice2007ColorScheme.Blue);
            }
            else if (clr == Color.Black)
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(mainForm, eOffice2007ColorScheme.Black);
            }
            else if (clr == Color.Silver)
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(mainForm, eOffice2007ColorScheme.Silver);
            }
            else
            {
                RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(mainForm, eOffice2007ColorScheme.Silver, clr);
            }

            //设置mainUI的前景色
            this.formColor = clr;

            ButtonItem parentItem = this.mainForm.menuStyle;

            //重新初始化菜单，使得选中项图标还原
            InitStyleMenu();

            //检查是否为自定义
            if (item == mainForm.menuStyleCustomize)
            {
                item.Checked = true;
                return;
            }

            //设定选中菜单的图标
            for (int i = 0; i < parentItem.SubItems.Count - 1; i++)
            {
                Image img = item.Image;
                Graphics g = Graphics.FromImage(img);
                Pen pen;

                if (parentItem.SubItems[i] == item)
                {
                    pen = new Pen(Color.Orange, 2);
                    g.DrawRectangle(pen, 1, 1, 14, 14);
                    break;
                }
            }
            //自定义菜单Checked=false;
            mainForm.menuStyleCustomize.Checked = false;

            //更改子窗体的颜色
            Function.SetFormStyle(overviewForm, clr);
        }

        /// <summary>
        /// 生成界面风格菜单前的图片
        /// </summary>
        /// <param name="img">图片</param>
        /// <param name="c">颜色</param>
        /// <param name="item">菜单项</param>
        private void ProduceStyleMenuImage(Image img, Color c, ButtonItem item)
        {
            Graphics g = Graphics.FromImage(img);
            Brush b = new SolidBrush(c);
            g.FillRectangle(b, 0, 0, 16, 16);

            item.Image = img;
            item.Refresh();
        }

        /// <summary>
        /// 初始化界面风格(&Y)的子菜单，为其添加图像
        /// </summary>
        private void InitStyleMenu()
        {
            Color color = new Color();
            Image bmp = new Bitmap(16, 16);
            Graphics g = Graphics.FromImage(bmp);

            //黑色
            color = Color.FromArgb(83, 83, 83);
            bmp = new Bitmap(16, 16);
            ProduceStyleMenuImage(bmp, color, mainForm.menuStyleBlack);
            mainForm.menuStyleBlack.Tag = Color.Black;

            //银色
            color = Color.FromArgb(229, 229, 238);
            bmp = new Bitmap(16, 16);
            ProduceStyleMenuImage(bmp, color, mainForm.menuStyleSilver);
            mainForm.menuStyleSilver.Tag = Color.Silver;

            //蓝色
            color = Color.FromArgb(191, 219, 255);
            bmp = new Bitmap(16, 16);
            ProduceStyleMenuImage(bmp, color, mainForm.menuStyleBlue);
            mainForm.menuStyleBlue.Tag = Color.Blue;

            //红色
            color = Color.FromArgb(252, 205, 222);
            bmp = new Bitmap(16, 16);
            ProduceStyleMenuImage(bmp, color, mainForm.menuStyleRed);
            mainForm.menuStyleRed.Tag = Color.Red;

            //绿色
            color = Color.FromArgb(205,229,222);
            bmp = new Bitmap(16, 16);
            ProduceStyleMenuImage(bmp, color, mainForm.menuStyleGreen);
            mainForm.menuStyleGreen.Tag = Color.Green;

            //金色
            color = Color.FromArgb(252, 245, 222);
            bmp = new Bitmap(16, 16);
            ProduceStyleMenuImage(bmp, color, mainForm.menuStyleGolden);
            mainForm.menuStyleGolden.Tag = Color.Gold;
        }
        #endregion


        /// <summary>
        /// 将Toolbar部分的按钮的Checked属性设置为false
        /// </summary>
        public void UncheckToolbar()
        {
            ButtonItem[] toolbarBtn = {mainForm.toolCmdPan,mainForm.toolCmdSelect,mainForm.toolCmdZoomIn,mainForm.toolCmdZoomOut,mainForm.toolCmdMeasure, mainForm.toolCmdIdentify };

            for (int i = 0; i < toolbarBtn.Length; i++)
            {
                toolbarBtn[i].Checked = false;
            }
        }

        /// <summary>
        /// 将相关菜单项的Checked属性设置为false
        /// </summary>
        public void UncheckMenu()
        {
            ButtonItem[] menu = {mainForm.menuPan, mainForm.menuSelection, mainForm.menuMeasureDistance, mainForm.menuIdentify };

            for (int i = 0; i < menu.Length; i++)
            {
                menu[i].Checked = false;
            }
        }

        #region Extent部分的按钮和菜单
        /// <summary>
        /// 设置前一个视图部分的菜单和工具栏按钮可用与否
        /// </summary>
        /// <param name="flag">项目是否可用</param>
        private void SetLastExtent(bool flag)
        {
            mainForm.menuBack.Enabled = flag;
            mainForm.toolCmdPrevExtent.Enabled = flag;
        }

        /// <summary>
        /// 设置下一个视图的菜单和工具栏按钮是否可用
        /// </summary>
        /// <param name="flag">是否可用</param>
        private void SetForwardExtent(bool flag)
        {
            mainForm.menuForward.Enabled = flag;
            mainForm.toolCmdNextExtent.Enabled = flag;
        }

        /// <summary>
        /// 设置主界面菜单工具栏Extent相关的项是否可用
        /// </summary>
        public void SetExtentItem()
        {
            SetForwardExtent(mainForm.mapOp.CanNextExtent());
            SetLastExtent(mainForm.mapOp.CanPrevExtent());
        }
        #endregion off

        /// <summary>
        /// 初始化设备管理工具栏的图层列表按钮的图像
        /// </summary>
        public void InitEquipmentGallery()
        {
            //ILayer lyr1 = frm.mapOp.GetLayerByName("封山育林");
            //ILayer lyr2 = frm.mapOp.GetLayerByName("地名");
            //ILayer lyr3 = frm.mapOp.GetLayerByName("铁路");

            //ButtonItem item = new ButtonItem("lyr1");
            //Bitmap bmp = new Bitmap(32, 32);
            //Function.GetLayerImage(lyr1 as IFeatureLayer, ref bmp);
            //item.Image = bmp;

            //frm.galleryEquipLayer.SubItems.Add(item);

            //暂时等待设备图层确定之后再写这部分代码，示例如上。。。
        }

        #region 缩略图部分功能
        /// <summary>
        /// 刷新缩略图的显示
        /// </summary>
        public void UpdateOverviewVisbleBounds()
        {
            if (mainForm.menuOverview.Checked)
            {
                overviewForm.overviewOp.RefreshForeground(null);
            }
        }

        /// <summary>
        /// 显示缩略图窗口
        /// </summary>
        public void ShowOverview()
        {
            overviewForm.Show();
        }

        /// <summary>
        /// 隐藏缩略图窗口
        /// </summary>
        public void HideOverview()
        {
            overviewForm.Hide();
            mainForm.menuOverview.Checked = false;
        }
        #endregion
    }
}
